/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Processor.ProcessControl;

import Data.ProcessData.ProcessData;
import Data.Frontier.URLCrl;
import Data.Frontier.URLObject;
import Processor.Components.ExtractURL;
import Processor.Components.ExtractContent;
import Processor.Components.FetchURL;
import Processor.Components.ContentSeen;
import Control.MainControl.MainControl;
import Data.ActivityLog.ActivityCrl;
import CrlException.CrlException;
import Config.ConfigSetting;
import ExtractorInterface.ExtractorInterface;
/**
 *
 * @author CUHUUHOANG
 */
public class ProcessThread extends Thread {

    boolean isSuspended;
    boolean isStopped;

    public ProcessThread() {
        isSuspended = false;
        isStopped = false;
    }

    @Override
    public void run() {
        try {
            //lay urlObj
            checkTheadSuspend();
            if (isStopped) {
                return;
            }
            URLObject urlObj = URLCrl.urlDT.getURL();
            if (urlObj == null) {
                removeThisThread();
                return;
            }
            //khoi tao process data
            checkTheadSuspend();
            if (isStopped) {
                return;
            }
            ProcessData pData = new ProcessData();
            pData.setUrl(urlObj.getLink());
            pData.setDepth(urlObj.getDepth());
            //fetch url
            checkTheadSuspend();
            if (isStopped) {
                return;
            }
            FetchURL fURL = new FetchURL(pData);
            if (!fURL.fetch()) {
                removeThisThread();
                return;
            }
            //create extractor           
            ExtractorInterface extractor = MainControl.createNewExtractor();
            extractor.setDocument(pData.getDoc());
            extractor.setURL(pData.getUrl());
            //content seen
            checkTheadSuspend();
            if (isStopped) {
                return;
            }
            ContentSeen ctSeen = new ContentSeen(extractor);
            if (ctSeen.isContentSeen()) {
                removeThisThread();
                return;
            }
            //extract content
            checkTheadSuspend();
            if (isStopped) {
                return;
            }
            ExtractContent exContent = new ExtractContent(pData, extractor);
            exContent.extractContent();
            //extract url
            if (isStopped) {
                return;
            }
            if(pData.getDepth()< ConfigSetting.depth||ConfigSetting.depth==0){
                ExtractURL exURL = new ExtractURL(pData, extractor);
                exURL.doExtractURL();
            }
            //finish
            removeThisThread();
        } catch (CrlException ex) {
            ActivityCrl.atiData.addAtivity(ex.getMessage());
        }
    }

    synchronized public void StopThread() throws CrlException {
        try {
            this.isStopped = true;
            this.isSuspended = false;
            notifyAll();
        } catch (Exception e) {
            throw new CrlException(e.getMessage());
        }
    }

    public void removeThisThread() throws CrlException {
        try {
            MainControl.ProcessManager.removeThread(this);
        } catch (Exception e) {
            throw new CrlException(e.getMessage());
        }

    }

    synchronized public void checkTheadSuspend() throws CrlException {
        
        try {
            if (isSuspended) {
                    wait();
            }
        } catch (Exception e) {
            throw new CrlException(e.getMessage());
        }

    }

    synchronized public void suspendThread(boolean isSuspend) throws CrlException {
        try {
            this.isSuspended = isSuspend;
            notifyAll();
        } catch (Exception e) {
            throw new CrlException(e.getMessage());
        }

    }
}
